﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RestaurantManagement.Controls;
using WindowsFormsView;
using WindowsFormsModel.BusinessObjects;
using WindowsFormsPresenter.UserGroup;
using WindowsFormsPresenter.Restaurant;
using System.IO;
using Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;

namespace RestaurantManagement.Modules.Systems
{
    public partial class uUserGroup : BaseModule, IUserGroupView, IUserGroupsView
    {
        #region Presenter

        private UserGroupPresenter _userGroupPresenter;
        private UserGroupsPresenter _userGroupsPresenter;

        #endregion

        private string _filePath = null;
        private byte[] _imageByte = null;

        #region Field

        public IList<UserGroupModel> UserGroups
        {
            set
            {
                var userGroups = value;
                AddUserGroupToGrid(userGroups);
            }
        }

        public string UserGroupID { get; set; }

        public string UserGroupName
        {
            get { return txtUserGroupName.Text.Trim(); }
            set { txtUserGroupName.Text = value; }
        }

        public byte[] Image
        {
            get
            {
                if (_filePath != null)
                {
                    if (picImage.Image != null)
                    {
                        using (MemoryStream stream = new MemoryStream())
                        {
                            picImage.Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                            return stream.ToArray();
                        }
                    }
                    else { return Image; }
                }
                else { return _imageByte; }
            }
            set
            {
                using (MemoryStream stream = new MemoryStream())
                {
                    picImage.Image = Utils.Image.FromByteToImage(value);
                    stream.Dispose();
                    _imageByte = value;
                    _filePath = null;
                }

            }
        }

        public string RestaurantID { get; set; }

        public string CreateBy
        {
            get { return txtCreateBy.Text; }
            set { txtCreateBy.Text = value; }
        }

        public DateTime CreateDate
        {
            get
            {
                if (dtCreateDate.EditValue != null) { return dtCreateDate.DateTime; }
                else { return dtCreateDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtCreateDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtCreateDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string ModifiedBy
        {
            get { return txtModifiedBy.Text; }
            set { txtModifiedBy.Text = value; }
        }

        public DateTime ModifiedDate
        {
            get
            {
                if (dtModifiedDate.EditValue != null) { return dtModifiedDate.DateTime; }
                else { return dtModifiedDate.Properties.MinValue; }
            }
            set
            {
                if (value.ToString().Equals("1/1/1753 12:00:00 AM")) { dtModifiedDate.EditValue = null; }
                else
                {
                    string[] date = value.ToString().Split('/');
                    dtCreateDate.EditValue = new DateTime(Convert.ToInt32(date[2].Substring(0, 4)), Convert.ToInt32(date[0]), Convert.ToInt32(date[1]));
                }
            }
        }

        public string Description
        {
            get { return txtDescription.Text; }
            set { txtDescription.Text = value; }
        }

        public bool IsActive
        {
            get { return chkIsActive.Checked; }
            set { chkIsActive.Checked = value; }
        }

        #endregion

        private void AddUserGroupToGrid(IList<UserGroupModel> userGroups)
        {
            if (userGroups != null)
            {
                grdMain.DataSource = userGroups;
                grdMain.ForceInitialize();

                for (int i = 0; i < grdMainView.Columns.Count; i++) { grdMainView.Columns[i].Visible = false; }

                grdMainView.Columns["UserGroupName"].Visible = true;
                grdMainView.Columns["CreateBy"].Visible = true;
                grdMainView.Columns["CreateDate"].Visible = true;
                grdMainView.Columns["IsActive"].Visible = true;

                grdMainView.Columns["UserGroupName"].Caption = "Tên nhóm";
                grdMainView.Columns["CreateBy"].Caption = "Người tạo";
                grdMainView.Columns["CreateDate"].Caption = "Ngày tạo";
                grdMainView.Columns["IsActive"].Caption = "Được sử dụng";

                grdMainView.Columns["UserGroupName"].VisibleIndex = 0;
                grdMainView.Columns["CreateBy"].VisibleIndex = 1;
                grdMainView.Columns["CreateDate"].VisibleIndex = 2;
                grdMainView.Columns["IsActive"].VisibleIndex = 3;
            }
            else
            {
                grdMain.DataSource = null;
                InitializeTextbox();
            }
        }

        private void InitializeTextbox()
        {
            txtCreateBy.Text = null;
            txtDescription.Text = null;
            txtModifiedBy.Text = null;
            txtUserGroupName.Text = null;
            dtCreateDate.EditValue = null;
            dtModifiedDate.EditValue = null;
            picImage.Image = null;
        }

        public uUserGroup()
        {
            InitializeComponent();

            _userGroupPresenter = new UserGroupPresenter(this);
            _userGroupsPresenter = new UserGroupsPresenter(this);

            XtraMessageBox.AllowCustomLookAndFeel = true;
        }

        private void uUserGroup_Load(object sender, EventArgs e)
        {
            try
            {
                _userGroupsPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UserGroupName))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                UserGroupID = null;
                _userGroupPresenter.Save();
                _userGroupsPresenter.Display();
                XtraMessageBox.Show("Thêm mới dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(UserGroupName))
            {
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            try
            {
                _userGroupPresenter.Save();
                _userGroupsPresenter.Display();
                XtraMessageBox.Show("Cập nhật dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (grdMainView.FocusedRowHandle >= 0)
                {
                    var userGroup = (UserGroupModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    new UserGroupPresenter(null).Delete(userGroup.UserGroupID);
                    _userGroupsPresenter.Display();
                    XtraMessageBox.Show("Xóa dữ liệu hoàn thành !", "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    XtraMessageBox.Show("Bạn chưa chọn dòng để xóa !", "Nhà hàng Hương Việt", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            try
            {
                _userGroupsPresenter.Display();
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.Title = "Chọn ảnh làm đại diện";

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                picImage.Image = System.Drawing.Image.FromFile(openFile.FileName);
                _filePath = openFile.FileName;
            }
            else { _filePath = null; };
        }

        private void grdMainView_RowClick(object sender, RowClickEventArgs e)
        {
            try
            {
                if (grdMainView.CalcHitInfo(e.Location).InRow && grdMainView.FocusedRowHandle >= 0)
                {
                    var userGroup = (UserGroupModel)grdMainView.GetRow(grdMainView.FocusedRowHandle);
                    _userGroupPresenter.Display(userGroup.UserGroupID);
                }
            }
            catch (ApplicationException ex) { XtraMessageBox.Show(ex.Message, "Nhà hàng Hương Việt !", MessageBoxButtons.OK, MessageBoxIcon.Error); }
        }

    }
}
